<!--  This script is specific to this page only. -->
<script type="text/javascript">

/**
 *  Toggles the checkboxes within the Race <=> Class Restriction table
 */ 
function toggleRaceClassRestrictions( cb ) {
   form = document.getElementById('raceClassRestrictions');
   
   // If he's checked, check all boxes and disable them.
   if ( cb.checked == true ) {
      for (i = 0; i < form.length; i++) {
         if ( form[i].type == "checkbox" &&
              form[i].id != "checkall" ) {
            form[i].checked = true;
            form[i].disabled = true;
         }
      }
   } else {  // Else, uncheck them all and enable them.
      for (i = 0; i < form.length; i++) {
         if ( form[i].type == "checkbox" &&
              form[i].id != "checkall" ) {
            form[i].checked = false;
            form[i].disabled = false;
         }
      }
   }
}

/**
 *  Enables all the checkboxes in the event the user resets the form while they are all disabled.
 */
function enableAll() {
   form = document.getElementById('raceClassRestrictions');

   for (i = 0; i < form.length; i++) {
      form[i].disabled = false;
   }
   
   return true;
}

/**
 *  Submits the Race <=> Class restriction table via AJAX methods.
 */
function submitRestrictions( form ) {
   
   var myCallback = callback;
   myCallback.setForm(form);
   myCallback.processSuccess = function (json) {
      // Nothing here yet.
   };
   
   var cObj = YAHOO.util.Connect.asyncRequest('POST', form.action, callback); 
   
   /* Because different browsers are screwy with memory, I'm cleaning it up */
   myCallback = null;
   form = null;
   
   return false;
}

</script>

<h1>Race and Class Options</h1>


<? $urlOptions = array ('module' => 'admin', 'controller' => 'class', 'action' => 'add'); ?>
<form id="class" action="<?= $this->url($urlOptions) ?>" onSubmit="return saveRow(this)" method="post">
<table id="classList" class="toonStatList" cellspacing="0" cellpadding="5">
   <thead>
      <tr>
         <th valign="middle" colspan="2">Classes</th>
      </tr>
   </thead>
   <tbody>
      <tr>
         <td valign="middle"><?= $this->formText('class_name', 'Add Class', array('size' => 15, 'onfocus' => "check_focus(this, 'Add Class')", 'onblur' => "check_blur(this, 'Add Class')")); ?></td>
         <td width="20px"><?= $this->formSubmit('submit', '+'); ?></td>
      </tr>
<? $i = 0;
$optionsCopy = $urlOptions; 
$optionsCopy['controller'] = 'confirm';
$optionsCopy['action'] = 'deleterow';
$optionsCopy['c'] = 'class';
foreach ( $this->classes as $row ): 
      $optionsCopy['i'] = $row['id'] ?>
      <tr id="class_<?= $row['id'] ?>" class="<?= $i++ % 2 ? 'even' : 'odd' ?>">
         <td><?= $row['name'] ?></td>
         <td style="text-align: center">
            <a href="<?= $this->url($optionsCopy)?>" onClick="return deleteRow('class','<?=$row['id']?>');"><img 
               id="class_<?=$row['id']?>_img" src="/tools/images/delete_16.png" border="0" height="16" width="16" /></a>
         </td>
      </tr>
<? endforeach; ?>
   </tbody>
</table>
</form>

<? $urlOptions = array ('module' => 'admin', 'controller' => 'race', 'action' => 'add'); ?>
<form id="race" action="<?= $this->url($urlOptions) ?>" onSubmit="return saveRow(this)" method="post">
<table id="raceList" class="toonStatList" cellspacing="0" cellpadding="5">
   <thead>
      <tr>
         <th valign="middle" colspan="2">Races</th>
      </tr>
   </thead>
   <tbody>
      <tr>
         <td><?= $this->formText('race_name', 'Add Race', array('size' => 15, 'onfocus' => "check_focus(this, 'Add Race')", 'onblur' => "check_blur(this, 'Add Race')")); ?></td>
         <td width="20px"><?= $this->formSubmit('submit', '+'); ?></td>
      </tr>
<? $i = 0;
$optionsCopy = $urlOptions; 
$optionsCopy['controller'] = 'confirm';
$optionsCopy['action'] = 'deleterow';
$optionsCopy['c'] = 'race';
   foreach ( $this->races as $row ):  
      $optionsCopy['i'] = $row['id'] ?>
      <tr id="race_<?= $row['id'] ?>" class="<?= $i++ % 2 ? 'even' : 'odd' ?>">
         <td><?= $row['name'] ?></td>
         <td style="text-align: center">
            <a href="<?= $this->url($optionsCopy)?>" onClick="return deleteRow('race','<?=$row['id']?>');"><img 
               id="race_<?=$row['id']?>_img" src="/tools/images/delete_16.png" border="0" height="16" width="16" /></a>
         </td>
      </tr>
<? endforeach; ?>
   </tbody>
</table>
</form>

<br class="clear" />


<h2>Race to Class Restrictions</h2>
<p class="footnote">* Check the boxes to restrict what race can play what class. No restrictions checks all boxes</p>
<form action="/admin/toon/raceclass" id="raceClassRestrictions" method="post" onsubmit="return submitRestrictions(this)">
<table>
   <thead>
   <tr>
      <td width="100px;" align="center">No Restrictions<br /><input type="checkbox" name="checkall" value="checkall" id="checkall" onclick="toggleRaceClassRestrictions(this)" /></td>
<? foreach ( $this->races as $race ): ?>
      <td width="50px" align="center"><?= $race['name']; ?></td>
<? endforeach; ?>
  </tr>
  </thead>
  <tbody>
<? $i = 0; foreach ( $this->classes as $class ): ?>
  <tr class="<?= $i++ % 2 ? 'even' : 'odd' ?>">
      <td><?= $class['name']; ?></td>
<?    foreach ( $this->races as $race ):  ?>
      <td align="center">
         <input type="checkbox" value="<?= $race['id'] . ':' . $class['id'] ?>" name="raceclass[]" id="rc_<?= $race['id'] . '_' . $class['id'] ?>" <?= in_array($race['id'] . ':' . $class['id'], $this->restrictions) ? 'checked="checked" ' : ' '; ?> />
      </td>
<?    endforeach; ?>
   </tr>
<? endforeach; ?>
   </tbody>
   <tfoot>
      <tr>
         <td colspan="4"><span id="statusMessage"><a href="./raceclass">Reload Table</a> (requires page reload)</span></td>
         <td colspan="<?= count($this->races) - 3 ?>" align="right">
            <?= $this->formReset( 'reset', 'Reset', array('onclick' => 'return enableAll()')); ?> &nbsp;
            <?= $this->formSubmit( 'save', 'Save Data' ); ?>
         </td>
      </tr>
   </tfoot>
</table>
</form>

<br />
<br />